package com.wq.struct;

/**
 * @Author: wangqiang20995
 * @Date:2019/2/19
 * @Description:
 * @Resource:
 */
public class StackStruct extends Struct {

    private int pop = -1;
    private int[] array;
    private int size;

    public StackStruct(int size) {
        array = new int[size];
        this.size = 0;
    }

    public void push(int element) {
        if (this.size == array.length) {
            throw new RuntimeException("堆栈已满");
        }

        this.array[++pop] = element;
        this.size++;
    }

    public int pop() {
        if (this.size == 0) {
            throw new RuntimeException("堆栈已空");
        }

        int ele = this.array[pop--];
        this.size--;
        return ele;
    }

    public int peek() {
        if (this.size == 0) {
            throw new RuntimeException("堆栈已空");
        }
        return this.array[pop];
    }

    public static void main(String args[]) {
        StackStruct stackStruct = new StackStruct(7);
        stackStruct.push(2);
        stackStruct.push(4);
        stackStruct.push(6);
        stackStruct.printStruct();
        System.out.println("弹出栈顶元素-->" + stackStruct.pop());
        stackStruct.printStruct();
        System.out.println("查看栈顶元素-->" + stackStruct.peek());
        stackStruct.printStruct();
    }

    @Override
    protected void printStruct() {
        for (int i = this.size - 1; i >= 0; i--) {
            System.out.printf("%d\t",this.array[i]);
        }
        System.out.printf("\n");
    }
}
